<template>
	<view class="content">
		<view class="uni-panel-c">
			<view class="uni-panel-bottom">
				<view class="uni-panel-item">
					<image class="uni-image" src="/static/img/back.png" mode="widthFix" @click="clickBack"></image>
					<view class="uni-panel-left">
						<text class="uni-text-item">你好，{{accountName}}同学，请点击要复习的生词本</text>
					</view>
				</view>
				<view class="uni-panel">
					<view class="uni-panel-book" v-for="(item, index) in listBook" :key="index" @click="clickBtnItem(item)">
						<image class="uni-word-image" :src="item.image" mode="widthFix" v-if='item.image'></image>
						<view class="uni-book-bottom" v-else>
							<view class="uni-text-word" >{{item.name}}({{item.wordsCount}}个)</view>
							<view class="uni-book-bottom-right">
								<image class="uni-word-image" src="/static/img/add.png" mode="widthFix" @click.stop="clickAdd(item)"></image>
								<image class="uni-word-image" src="/static/img/delete.png" mode="widthFix" @click.stop="clickDelete(item)"></image>
								<image class="uni-word-image" src="/static/img/edit.png" mode="widthFix" @click.stop="clickEdit(item)"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getNewWordList, addNewWordBook, editNewWordBook, deleteNewWordBook } from '@/api/api.js';
	import store from "../../store";
	import { HTTP_REQUEST_URL } from '@/config/app.js';
	export default {
		mounted() {
		        // #ifdef APP-PLUS
		        plus.screen.lockOrientation('landscape');
		        // #endif
		    },
		data() {
			return {
				userId: 0,
				accountName: '',
				user_type: 0,
				listBook: [],
			}
		},
		onLoad(options) {
		},
		onShow(){
			const userInfo = store.state.app.userInfo;
			if( userInfo){
				this.accountName = userInfo.nickname;
				this.userId = userInfo.id;
				this.user_type = userInfo.user_type;
			}
			let student = uni.getStorageSync('student_info');
			if( student){
				this.accountName = student.nickname;
				this.userId = student.id;
			}
				
			this.innerGetNewWordList();
		},
		methods: {
			innerGetNewWordList(){
				let that = this;
				this.listBook = [];
				let params = {user_id: this.userId, book_id: 0, is_new: true};
				getNewWordList(params).then(res => {
					let ret = [];
					if( 0 == res.code && res.data ){
						for( let item of res.data.rows){
							item.wordsCount = 0;
							if( item.words){
								item.wordsCount = item.words.split(',').length;
							}
							ret.push(item);
						}
						ret.push({image: '/static/img/add_book.png'});
					}
					that.listBook = ret;
				}).catch(error => {
					// 处理异常
					console.error('发生错误:', error);
				});
			},
			clickBack(){
				uni.navigateBack();
			},
			clickBtnItem(item){
				if( item.id){
					let toParams = {user_id: this.userId, newbook_id: item.id, isNewWord:true}
					let params = Object.keys(toParams)
						.map(key => `${key}=${encodeURIComponent(toParams[key])}`)
						.join('&');
					uni.navigateTo({
						url: '/pages/studyex/index?' + params
					});
				}else{
					let self = this;
					uni.showModal({
					    title: '请输入添加的生词本名称',
					    editable: true,
					    success: function (res) {
					        if (res.confirm && res.content) {
								self.innerAdd(res.content);
					        }
					    }
					});
				}
			},
			clickAdd(item){
				console.log(item)
				uni.navigateTo({
					url: '/pages/customword/index?newbook_id=' + item.id
				});
			},
			clickDelete(item){
				let self = this;
				uni.showModal({
				    title: '提示',
				    content: '确认要删除这个生词本吗？',
				    success: function (res) {
				        if (res.confirm) {
				            self.innerDelete(item.id);
				        }
				    }
				});
			},
			clickEdit(item){
				let self = this;
				uni.showModal({
				    title: '请输入修改的生词本名称',
				    editable: true,
					content: item.name,
				    success: function (res) {
				        if (res.confirm && res.content) {
							self.innerEdit( item.id, res.content);
				        }
				    }
				});
			},
			innerDelete(id){
				let params = {id};
				deleteNewWordBook(params).then(res => {
					//console.log(res)
					if( res.code == 0){
						this.innerGetNewWordList();
					}else{
						uni.showToast({
						    title: res.msg,
							icon: 'none',
						    duration: 2000
						});
					}
				}).catch(err => {
					uni.showToast({
					    title: err,
						icon: 'none',
					    duration: 2000
					});
				});
			},
			innerAdd(name){
				let params = {user_id: this.userId, name};
				addNewWordBook(params).then(res => {
					//console.log(res)
					if( res.code == 0){
						this.innerGetNewWordList();
					}else{
						uni.showToast({
						    title: res.msg,
							icon: 'none',
						    duration: 2000
						});
					}
				}).catch(err => {
					uni.showToast({
					    title: err,
						icon: 'none',
					    duration: 2000
					});
				});
			},
			innerEdit(id, name){
				let params = {id, name};
				editNewWordBook(params).then(res => {
					//console.log(res)
					if( res.code == 0){
						this.innerGetNewWordList();
					}else{
						uni.showToast({
						    title: res.msg,
							icon: 'none',
						    duration: 2000
						});
					}
				}).catch(err => {
					uni.showToast({
					    title: err,
						icon: 'none',
					    duration: 2000
					});
				});
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
	.content {
		width: 100%;
		height: 100%;
		background-image: url('../../static/img/bg.jpg');
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.uni-panel-c {
		width: 90%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	
	.uni-panel-top {
		width: 100%;
		height: 20%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}
	.uni-image {
		width: 6%;
	}
	.uni-text-logo{
		color: black;
		font-size: 10vmin;
		margin-left: 2%;
		text-align: center;
		font-weight: bold;
		letter-spacing: 1rpx;
		font-family: "楷体";
	}
	
	.uni-panel-bottom-bk {
		position: absolute;
		left: 5%;
		top: 20%;
		width: 90%;
		height: 80%;
		background-color: white;
		opacity: 0.3;
		border-radius: 40rpx;
	}
	.uni-panel-bottom {
		margin-top: 8%;
		width: 100%;
		height: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;
		z-index: 1;
	}
	.uni-panel-item {
		width: 96%;
		height: 15%;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.uni-panel-left {
		display: flex;
		flex-direction: column;
		margin-left: 2%;
	}
	
	.uni-text-item{
		font-size: 5vmin;
		color: white;
		font-weight: bold;
		letter-spacing: 1rpx;
		text-shadow:
		    -1px -1px 0 #000,
		    1px -1px 0 #000,
		    -1px 1px 0 #000,
		    1px 1px 0 #000; /* 调整偏移量和颜色来设置描边宽度和颜色 */
	}
	
	.uni-panel {
		width:100%;
		height: 85%;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		overflow: auto;
	}
	
	.uni-panel-book {
		background-image: url('../../static/img/package.png');
		background-size: 100% 100%;
		height: 40%;
		width: 24%;
		margin-left: 1%;
		margin-top: 1%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
	}
	
	.uni-word-image {
		width: 20%;
		margin: 0 2%;
	}
	
	.uni-text-word{
		font-size: 3vmin;
		color: black;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.uni-book-bottom{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 3%;
		width: 100%;
	}
	
	.uni-book-bottom-right{
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 100%;
		margin-top: 5%;
	}
	
	.uni-text-word-right{
		font-size: 2vmin;
		color: black;
	}
	
	.progress-box{
		width: 90%;
	}
</style>
